package com.zt.weibo.main;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zt.weibo.R;
import com.zt.weibo.utils.AsyncImageLoader;
import com.zt.weibo.utils.FormatString;
import com.zt.weibo.utils.PrintLog;
import com.zt.weibo.vo.StatusInfo;

public class StatusAdapter extends BaseAdapter
{
	private AsyncImageLoader mIconLoader = AsyncImageLoader.getInstance();
	private ArrayList<StatusInfo> mList = null;
	private Context mContext = null;
	private ImgClickListener mListener = null;
	private WeiboImgPopwin mImgPopwin = null;
	private class ViewHolder
	{
		/**
		 * 用户名
		 */
		private TextView mUserName;
		
		/**
		 * 用户头像
		 */
		private ImageView mUserPic;
		
		/**
		 * 微博内容
		 */
		private TextView mStatusContent;
		
		/**
		 * 微博图片
		 */
		private LinearLayout mStatusPic;
		
		/**
		 * 发布时间
		 */
		private TextView mCreatTime;
		
		/**
		 * 被转发微博框架
		 */
		private LinearLayout mRetweetLayout;
		
		/**
		 * 被转发微博用户名
		 */
		private TextView mRetUserName;
		
		/**
		 * 被转发微博内容
		 */
		private TextView mRetStatusContent;
		
		/**
		 * 被转发微博图片
		 */
		private LinearLayout mRetStatusPic;
		
		/**
		 * 被转发微博转发、评论数
		 */
		private TextView mRetStatusState;
		
		/**
		 * 被转发微博被删除提示
		 */
		private TextView mRetBeenDelet;
		
		/**
		 * 微博来自
		 */
		private TextView mComefrom;
		
		/**
		 * 微博转发、评论数
		 */
		private TextView mStatusState;
		
		private void resetView()
		{
			mUserName.setText("");
			mUserPic.setImageResource(R.drawable.icon);
			mUserPic.setTag(null);
			mStatusContent.setText("");
			mStatusPic.setVisibility(View.GONE);
			((ImageView)mStatusPic.getChildAt(0)).setImageResource(R.drawable.icon);
			mStatusPic.setTag(null);
			mCreatTime.setText("");
			mRetweetLayout.setVisibility(View.GONE);
			mRetUserName.setText("");
			mRetStatusContent.setText("");
			mRetStatusPic.setVisibility(View.GONE);
			((ImageView)mStatusPic.getChildAt(0)).setImageResource(R.drawable.icon);
			mRetStatusPic.setTag(null);
			mRetStatusState.setText("");
			mRetBeenDelet.setVisibility(View.GONE);
			mComefrom.setText("");
			mStatusState.setText("");
		}
	}
	
	public StatusAdapter(Context con)
	{
		mContext = con;
		mIconLoader.clearCache();
		mListener = new ImgClickListener();
		mImgPopwin = new WeiboImgPopwin(mContext, LayoutInflater.from(mContext)
				.inflate(R.layout.weibo_main, null));
	}
	
	public void setStatusList(ArrayList<StatusInfo> statusList)
	{
		mList = statusList;
	}
	
	/**
	 * 刷新最新微博
	 * @param statusList
	 * @param bIsWantNewer true表示需要新微薄，flase表示需要老微博。
	 */
	public void updateStatusList(ArrayList<StatusInfo> statusList, boolean bIsWantNewer)
	{
		if(statusList != null)
		{
			if(bIsWantNewer)
			{
				int i = statusList.size() - 1;
				while(i >= 0)
				{
					mList.add(0, statusList.get(i));
					i --;
				}
			}
			else
			{
				int i = 1;
				while(i < statusList.size())
				{
					mList.add(statusList.get(i));
					i ++;
				}
			}
		}
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount()
	{
		return mList.size();
	}

	@Override
	public StatusInfo getItem(int position)
	{
		return mList.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder mHolder = null;
		StatusInfo tempInfo = null;
		if(convertView == null)
		{
			mHolder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.status_list_item, null);
			findView(mHolder, convertView);
			convertView.setTag(mHolder);
		}
		else
		{
			mHolder = (ViewHolder)convertView.getTag();
			mHolder.resetView();
		}
		
		tempInfo = mList.get(position);
		String strUserUrl = tempInfo.mUserInfo.strProfileImageUrl;
		String strStatusUrl = tempInfo.strThumbnailPic;
		String strStatusState = String.format(mContext.getString(R.string.status_state), tempInfo.iRepostsCount, 
				tempInfo.iCommentsCount);
		mHolder.mUserName.setText(FormatString.formatRemark(tempInfo.mUserInfo));
		
		if(!"".equals(strUserUrl))
		{
			mHolder.mUserPic.setTag(tempInfo.mUserInfo.strAvatarLargeImageUrl);
			mIconLoader.loadImage(strUserUrl, mHolder.mUserPic);
			mHolder.mUserPic.setOnClickListener(mListener);
		}
		
		mHolder.mStatusContent.setText(tempInfo.strContent);
		
		if(!"".equals(strStatusUrl))
		{
			mHolder.mStatusPic.setTag(tempInfo.strOriginalPic);
			mIconLoader.loadImage(strStatusUrl, mHolder.mStatusPic);
			mHolder.mStatusPic.setOnClickListener(mListener);
		}
		
		mHolder.mCreatTime.setText(tempInfo.strCreatTime);
		mHolder.mComefrom.setText(mContext.getString(R.string.come_from) + tempInfo.strSource);
		mHolder.mStatusState.setText(strStatusState);
		
		if(tempInfo.bIsRetweetOther)
		{
			tempInfo = tempInfo.mRetweetInfo;
			if(1 == tempInfo.iDeleted)
			{
				mHolder.mRetBeenDelet.setVisibility(View.VISIBLE);
				return convertView;
			}
			String mRetStatusUrl = tempInfo.strThumbnailPic;
			String strRetStatusState = String.format(mContext.getString(R.string.status_state), 
					tempInfo.iRepostsCount, tempInfo.iCommentsCount);
			mHolder.mRetweetLayout.setVisibility(View.VISIBLE);
			mHolder.mRetUserName.setText("@" + FormatString.formatRemark(tempInfo.mUserInfo));
			mHolder.mRetStatusContent.setText(tempInfo.strContent);
			if(!"".equals(mRetStatusUrl))
			{
				mHolder.mRetStatusPic.setTag(tempInfo.strOriginalPic);
				mIconLoader.loadImage(mRetStatusUrl, mHolder.mRetStatusPic);
				mHolder.mRetStatusPic.setOnClickListener(mListener);
			}
			mHolder.mRetStatusState.setText(strRetStatusState);
		}
		return convertView;
	}
	
	private void findView(ViewHolder mHolder, View convertView)
	{
		mHolder.mUserName = (TextView) convertView.findViewById(R.id.user_name_text);
		mHolder.mUserPic = (ImageView) convertView.findViewById(R.id.user_pic_img);
		mHolder.mStatusContent = (TextView) convertView.findViewById(R.id.status_content_text);
		mHolder.mStatusPic = (LinearLayout) convertView.findViewById(R.id.status_pic_img);
		mHolder.mCreatTime = (TextView) convertView.findViewById(R.id.status_time_text);
		mHolder.mRetUserName = (TextView) convertView.findViewById(R.id.retweet_user_name_text);
		mHolder.mRetStatusContent = (TextView) convertView.findViewById(R.id.retweet_status_content_text);
		mHolder.mRetStatusPic = (LinearLayout) convertView.findViewById(R.id.retweet_status_pic_img);
		mHolder.mRetStatusState = (TextView) convertView.findViewById(R.id.retweet_status_state_text);
		mHolder.mRetweetLayout = (LinearLayout) convertView.findViewById(R.id.retweet_status_layout);
		mHolder.mRetBeenDelet = (TextView) convertView.findViewById(R.id.retweet_status_delet_hint);
		mHolder.mComefrom = (TextView) convertView.findViewById(R.id.status_come_from_text);
		mHolder.mStatusState = (TextView) convertView.findViewById(R.id.status_state_text);
	}
	
	private class ImgClickListener implements OnClickListener
	{
		@Override
		public void onClick(View v)
		{
			switch(v.getId())
			{
			case R.id.user_pic_img:
				PrintLog.debug("点击了用户头像");
				mImgPopwin.show((String)v.getTag());
				break;
			case R.id.status_pic_img:
				PrintLog.debug("点击了微博配图");
				mImgPopwin.show((String)v.getTag());
				break;
			case R.id.retweet_status_pic_img:
				PrintLog.debug("点击了转发微博配图");
				mImgPopwin.show((String)v.getTag());
				break;
			}
		}
	}
	
}
